<template>
  <div class="question-list-container">
    <div class="content-wrapper">
      <h1 class="page-title animate__animated animate__fadeInDown">
        <t-icon name="help-circle" class="title-icon" />
        最新问题
      </h1>
      
      <div class="filter-bar animate__animated animate__fadeIn">
        <div class="filter-tags">
          <span class="filter-tag active">最新</span>
          <span class="filter-tag">热门</span>
          <span class="filter-tag">等待回答</span>
        </div>
        <div class="search-box">
          <t-icon name="search" class="search-icon" />
          <input type="text" placeholder="搜索问题..." class="search-input" />
        </div>
      </div>
      
      <div class="question-list" v-infinite-scroll="loadMore" :infinite-scroll-disabled="loading" infinite-scroll-distance="50">
        <div v-for="question in questions" :key="question.id" class="question-card animate__animated animate__fadeIn">
          <div class="question-content">
            <div class="question-header">
              <h2 class="question-title" @click="$router.push(`/home/question/${question.id}`)">{{ question.title }}</h2>
              <div class="question-tags" v-if="question.tags && question.tags.length">
                <span v-for="tag in question.tags" :key="tag" class="tag">{{ tag }}</span>
              </div>
            </div>
            
            <p class="question-excerpt" v-if="question.content">
              {{ truncateText(stripHtml(question.content), 150) }}
            </p>
            
            <div class="question-meta">
              <div class="user-info" v-if="question.author">
                <img :src="question.author.avatar || '/default-avatar.png'" alt="用户头像" class="user-avatar" />
                <span class="username">{{ question.author.username || '匿名用户' }}</span>
              </div>
              
              <span class="time">
                <t-icon name="time" class="icon-time" />
                {{ formatDate(question.createdAt) }}
              </span>
              
              <div class="interaction-stats">
                <span class="stat">
                  <t-icon name="thumb-up" class="icon-like" />
                  {{ question.likeCount }}
                </span>
                <span class="stat">
                  <t-icon name="star" class="icon-star" />
                  {{ question.favoriteCount }}
                </span>
                <span class="stat">
                  <t-icon name="chat" class="icon-comment" />
                  {{ question.answerCount }}
                </span>
              </div>
            </div>
          </div>
        </div>
        
        <div v-if="loading" class="loading-more">
          <el-spinner size="medium" />
          <span>正在加载更多内容...</span>
        </div>
        
        <div v-if="noMore" class="no-more">
          <div class="divider">
            <span>已经到底啦 ~</span>
          </div>
        </div>
      </div>
    </div>
    
    <div class="floating-action-button" @click="$router.push('/home/ask')">
      <t-icon name="plus" class="fab-icon" />
      <span class="fab-text">提问</span>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getQuestionsOrderByDESC } from '@/api/questionApi'
import 'animate.css'

const questions = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const loading = ref(false)
const noMore = ref(false)

const fetchQuestions = async () => {
  if (loading.value || noMore.value) return
  
  loading.value = true
  try {
    const response = await getQuestionsOrderByDESC(currentPage.value, pageSize.value)
    const newQuestions = response.data
    
    if (newQuestions.length === 0) {
      noMore.value = true
    } else {
      questions.value = [...questions.value, ...newQuestions]
      total.value = response.data.total
    }
  } catch (error) {
    console.error('Failed to fetch questions:', error)
  } finally {
    loading.value = false
  }
}

const loadMore = () => {
  if (!loading.value && !noMore.value) {
    currentPage.value++
    fetchQuestions()
  }
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  const now = new Date()
  const diff = now - date
  
  // 如果是当前分钟内
  if (diff < 60 * 1000) {
    const seconds = Math.floor(diff / 1000)
    return `${seconds}秒前`
  }
  
  // 如果是当前小时内
  if (diff < 60 * 60 * 1000) {
    const minutes = Math.floor(diff / (60 * 1000))
    return `${minutes}分钟前`
  }
  
  // 如果是当天
  if (diff < 24 * 60 * 60 * 1000) {
    const hours = Math.floor(diff / (60 * 60 * 1000))
    return `${hours}小时前`
  }
  
  // 如果是今年
  if (date.getFullYear() === now.getFullYear()) {
    return `${date.getMonth() + 1}月${date.getDate()}日`
  }
  
  // 如果是往年
  return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
}

const truncateText = (text, maxLength) => {
  if (!text) return '';
  if (text.length <= maxLength) return text;
  return text.substring(0, maxLength) + '...';
};

const stripHtml = (html) => {
  if (!html) return '';
  return html.replace(/<[^>]*>/g, '');
};

onMounted(() => {
  fetchQuestions()
})
</script>

<style scoped>
.question-list-container {
  padding: 40px 0;
  min-height: calc(100vh - 100px);
  background-color: #f8f9fa;
  width: 100%;
  position: relative;
}

.content-wrapper {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-title {
  font-size: 2.5rem;
  color: #2d3748;
  margin-bottom: 30px;
  font-weight: 800;
  display: flex;
  align-items: center;
  letter-spacing: -0.5px;
}

.title-icon {
  margin-right: 12px;
  color: #4299e1;
  font-size: 2rem;
}

.filter-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: white;
  border-radius: 12px;
  padding: 12px 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.filter-tags {
  display: flex;
  gap: 12px;
}

.filter-tag {
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #edf2f7;
  color: #4a5568;
}

.filter-tag:hover {
  background: #e2e8f0;
}

.filter-tag.active {
  background: #4299e1;
  color: white;
  font-weight: 500;
}

.search-box {
  position: relative;
  width: 250px;
}

.search-input {
  width: 100%;
  padding: 10px 16px 10px 40px;
  border-radius: 20px;
  border: 1px solid #e2e8f0;
  background: #f7fafc;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.search-input:focus {
  outline: none;
  border-color: #4299e1;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.15);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: #a0aec0;
}

.question-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;
}

.question-card {
  background: white;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  transition: all 0.4s cubic-bezier(0.165, 0.84, 0.44, 1);
  border: 1px solid rgba(0, 0, 0, 0.03);
  overflow: hidden;
  position: relative;
}

.question-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(180deg, #4299e1, #63b3ed);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.question-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.question-card:hover::before {
  opacity: 1;
}

.question-content {
  padding-left: 10px;
}

.question-header {
  margin-bottom: 16px;
}

.question-title {
  font-size: 1.8rem;
  color: #1a202c;
  margin-bottom: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: color 0.3s ease;
  line-height: 1.3;
}

.question-title:hover {
  color: #4299e1;
}

.question-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.tag {
  padding: 4px 12px;
  background: #ebf8ff;
  color: #3182ce;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 500;
}

.question-excerpt {
  color: #4a5568;
  line-height: 1.6;
  margin-bottom: 20px;
  font-size: 1rem;
}

.question-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #718096;
  font-size: 0.9rem;
  border-top: 1px solid #edf2f7;
  padding-top: 16px;
  margin-top: 10px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  margin-right: 8px;
  object-fit: cover;
}

.username {
  font-weight: 500;
  color: #4a5568;
}

.time {
  display: flex;
  align-items: center;
}

.icon-time {
  margin-right: 6px;
  color: #a0aec0;
}

.interaction-stats {
  display: flex;
  gap: 16px;
}

.stat {
  display: flex;
  align-items: center;
}

.icon-like, .icon-star, .icon-comment {
  margin-right: 6px;
}

.icon-like {
  color: #f56565;
}

.icon-star {
  color: #ecc94b;
}

.icon-comment {
  color: #4299e1;
}

.loading-more {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 30px 0;
  color: #718096;
  gap: 12px;
}

.no-more {
  padding: 30px 0;
}

.divider {
  display: flex;
  align-items: center;
  color: #a0aec0;
  font-size: 0.9rem;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  border-bottom: 1px solid #e2e8f0;
}

.divider span {
  margin: 0 16px;
}

.floating-action-button {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: auto;
  height: 56px;
  border-radius: 28px;
  background: #4299e1;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(66, 153, 225, 0.4);
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 0 24px;
  z-index: 100;
}

.floating-action-button:hover {
  background: #3182ce;
  transform: translateY(-3px);
  box-shadow: 0 6px 24px rgba(66, 153, 225, 0.5);
}

.fab-icon {
  margin-right: 8px;
  font-size: 1.2rem;
}

.fab-text {
  font-weight: 600;
  font-size: 1rem;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .filter-bar {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .search-box {
    width: 100%;
  }
  
  .question-title {
    font-size: 1.5rem;
  }
  
  .question-meta {
    flex-wrap: wrap;
    gap: 12px;
  }
  
  .floating-action-button {
    width: 56px;
    padding: 0;
  }
  
  .fab-text {
    display: none;
  }
  
  .fab-icon {
    margin-right: 0;
  }
}
</style>